package cn.iocoder.yudao.module.pay.controller.admin.pricingrules;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pay.controller.admin.pricingrules.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.pricingrules.PricingRulesDO;
import cn.iocoder.yudao.module.pay.service.pricingrules.PricingRulesService;

@Tag(name = "管理后台 - 用于支付模型翻译的价格套餐列")
@RestController
@RequestMapping("/pay/pricing-rules")
@Validated
public class PricingRulesController {

    @Resource
    private PricingRulesService pricingRulesService;

    @PostMapping("/create")
    @Operation(summary = "创建用于支付模型翻译的价格套餐列")
    @PreAuthorize("@ss.hasPermission('pay:pricing-rules:create')")
    public CommonResult<Integer> createPricingRules(@Valid @RequestBody PricingRulesSaveReqVO createReqVO) {
        return success(pricingRulesService.createPricingRules(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用于支付模型翻译的价格套餐列")
    @PreAuthorize("@ss.hasPermission('pay:pricing-rules:update')")
    public CommonResult<Boolean> updatePricingRules(@Valid @RequestBody PricingRulesSaveReqVO updateReqVO) {
        pricingRulesService.updatePricingRules(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用于支付模型翻译的价格套餐列")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pay:pricing-rules:delete')")
    public CommonResult<Boolean> deletePricingRules(@RequestParam("id") Integer id) {
        pricingRulesService.deletePricingRules(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用于支付模型翻译的价格套餐列")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:pricing-rules:query')")
    public CommonResult<PricingRulesRespVO> getPricingRules(@RequestParam("id") Integer id) {
        PricingRulesDO pricingRules = pricingRulesService.getPricingRules(id);
        return success(BeanUtils.toBean(pricingRules, PricingRulesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用于支付模型翻译的价格套餐列分页")
    @PreAuthorize("@ss.hasPermission('pay:pricing-rules:query')")
    public CommonResult<PageResult<PricingRulesRespVO>> getPricingRulesPage(@Valid PricingRulesPageReqVO pageReqVO) {
        PageResult<PricingRulesDO> pageResult = pricingRulesService.getPricingRulesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PricingRulesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用于支付模型翻译的价格套餐列 Excel")
    @PreAuthorize("@ss.hasPermission('pay:pricing-rules:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPricingRulesExcel(@Valid PricingRulesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PricingRulesDO> list = pricingRulesService.getPricingRulesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用于支付模型翻译的价格套餐列.xls", "数据", PricingRulesRespVO.class,
                        BeanUtils.toBean(list, PricingRulesRespVO.class));
    }

}